# Maintainer: Nazar Mishturak <nazar m x at gmail dot com>

_realname=librsync
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.3.2
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url='https://librsync.github.io/'
pkgdesc="Remote delta-compression library. (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-popt")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-doxygen")
license=('LGPL2')
options=('strip' 'staticlibs' 'docs')
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/librsync/librsync/archive/v${pkgver}.tar.gz")
sha256sums=('ef8ce23df38d5076d25510baa2cabedffbe0af460d887d86c2413a1c2b0c676f')

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  # Static
  [[ -d ${srcdir}/build-${MINGW_CHOST}-static ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}-static
  mkdir -p ${srcdir}/build-${MINGW_CHOST}-static && cd ${srcdir}/build-${MINGW_CHOST}-static
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_INSTALL_LIBDIR=lib \
    -DBUILD_SHARED_LIBS=OFF \
    "${extra_config[@]}" \
    ../${_realname}-${pkgver}
  make

  # Shared
  [[ -d ${srcdir}/build-${MINGW_CHOST}-shared ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}-shared
  mkdir -p ${srcdir}/build-${MINGW_CHOST}-shared && cd ${srcdir}/build-${MINGW_CHOST}-shared

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_INSTALL_LIBDIR=lib \
    -DBUILD_SHARED_LIBS=ON \
    "${extra_config[@]}" \
    ../${_realname}-${pkgver}
  make
  # Generate docs
  make doc
}

package () {
  # Static
  cd "${srcdir}/build-${MINGW_CHOST}-static"
  make DESTDIR=${pkgdir} install
  
  # Shared
  cd "${srcdir}/build-${MINGW_CHOST}-shared"
  make DESTDIR=${pkgdir} install

  # Install docs
  mkdir -p "${pkgdir}/${MINGW_PREFIX}/share/doc/${_realname}"
  cp -a html "${pkgdir}/${MINGW_PREFIX}/share/doc/${_realname}"
}
